<?php

namespace app\index\controller;

/**
 * Description of UserAdmin
 * 用户管理
 * @author Dongasai
 */
class UserAdmin extends \think\Controller
{

    public function index()
    {
        $re = $this->isAdmin();
        if (is_string($re)) {
            return $this->error($re);
        }
        //读取用户列表
        $where = [];
        $order = 'id desc';
        $page  = input('page', 1, 'int');
        $low   = 10;

        $data = \app\index\service\User::listForPage($where, $order, $page, $low);

        $this->assign('user', dataToPagedata($data));

        return $this->fetch();
    }

    /**
     * 检查是否是管理员
     * @return boolean
     */
    protected function isAdmin()
    {
        if (!is_login()) {
            return "没有登陆!";
        }
        $info = $this->get('userinfo');
        if ($info['auth'] == 0) {
            return true;
        } else {
            return "不是管理员";
        }
    }

    /**
     * 编辑项目
     */
    public function edit()
    {
        $uid = session("uid");
        if (request()->ispost()) {
            $to_uid = input('to_uid', 0, 'int');
            $data   = $_POST;
            unset($data['to_uid']);
            $re     = \app\index\service\User::edit($uid, $to_uid, $data);
            if (is_string($re)) {
                return $this->error($re);
            } else {
                return $this->success("成功");
            }
        } else {
            $id   = input("id", 0, 'int');
            $info = \app\index\service\User::userinfo($id);
            $this->assign('edit_u_info', $info);
            return $this->fetch();
        }
    }

    /**
     * 增加用户
     * @return type
     */
    public function add()
    {
        $uid = session('uid');
        if (request()->ispost()) {
            dump($_POST);
            $data = $_POST;
            $re   = \app\index\service\User::add($uid, $data);
            if (is_string($re)) {
                return $this->error($re);
            } else {
                return $this->success("成功");
            }
        } else {
            return $this->fetch();
        }
    }

}
